package dao;

import org.apache.ibatis.annotations.*;
import pojo.User;
import java.util.List;

@Mapper
public interface UserMapper {
    // 查询方法（带字段映射）
    @Select("SELECT id, username, password, phone, email, " +
            "CASE WHEN status = 1 THEN false ELSE true END AS isLocked, " +
            "create_time AS createTime FROM yh WHERE id = #{id}")
    User findUserById(Integer id);

    @Select("SELECT id, username, password, phone, email, " +
            "CASE WHEN status = 1 THEN false ELSE true END AS isLocked, " +
            "create_time AS createTime FROM yh WHERE username = #{username}")
    User findUserByUsername(String username);

    @Select("SELECT id, username, password, phone, email, " +
            "CASE WHEN status = 1 THEN false ELSE true END AS isLocked, " +
            "create_time AS createTime FROM yh")
    List<User> findAllUsers();

    // 增删改方法
    @Insert("INSERT INTO yh (username, password, phone, email, status) " +
            "VALUES (#{username}, #{password}, #{phone}, #{email}, " +
            "CASE WHEN #{isLocked} = true THEN 0 ELSE 1 END)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addUser(User user);

    @Update("UPDATE yh SET username=#{username}, password=#{password}, " +
            "phone=#{phone}, email=#{email}, " +
            "status=CASE WHEN #{isLocked} = true THEN 0 ELSE 1 END " +
            "WHERE id=#{id}")
    int updateUser(User user);

    @Delete("DELETE FROM yh WHERE id = #{id}")
    int deleteUser(Integer id);

    @Update("UPDATE yh SET status = CASE WHEN #{isLocked} = true THEN 0 ELSE 1 END " +
            "WHERE id = #{id}")
    int lockUser(@Param("id") Integer id, @Param("isLocked") Boolean isLocked);
}